package com.sunlands.qdcy.deskmate.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Map;

@Slf4j
public class ElemeHelper {
    private static String concatParams(Map<String, String> params2) throws UnsupportedEncodingException {
        Object[] key_arr = params2.keySet().toArray();
        Arrays.sort(key_arr);
        String str = "";

        for (Object key : key_arr) {
            String val = params2.get(key);
            //key = URLEncoder.encode(key.toString(), "UTF-8");
            //val = URLEncoder.encode(val, "UTF-8");
            str += "&" + key + "=" + val;
        }

        return str.replaceFirst("&", "");
    }

    private static String concatParams3(Map<String, String> params2) throws UnsupportedEncodingException {
        Object[] key_arr = params2.keySet().toArray();
        Arrays.sort(key_arr);
        String str = "";

        for (Object key : key_arr) {
            String val = params2.get(key)==null?"":params2.get(key);
            //key = URLEncoder.encode(key.toString(), "UTF-8");
            //val = URLEncoder.encode(val, "UTF-8");
            str += "&" + key + "=" + val;
        }

        return str.replaceFirst("&", "");
    }


    private static String concatParams1(Map<String, Object> params2) throws UnsupportedEncodingException {
//        log.info("concatParams1 params2="+params2==null?"null":"notnull");

        Object[] key_arr = params2.keySet().toArray();
        Arrays.sort(key_arr);
        String str = "";

        for (Object key : key_arr) {
            String val = String.valueOf(params2.get(key));
            //key = URLEncoder.encode(key.toString(), "UTF-8");
            //val = URLEncoder.encode(val, "UTF-8");
            str += "&" + key.toString() + "=" + val;
        }

        return str.replaceFirst("&", "");
    }

    private static String byte2hex(byte[] b) {
        StringBuffer buf = new StringBuffer();
        int i;

        for (int offset = 0; offset < b.length; offset++) {
            i = b[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }

        return buf.toString();
    }

    public static String getSig( Map<String, String> params)  {
        String str = null;
        try {
            String p = concatParams(params);
            if(StringUtils.isEmpty(p)){
                str = "apikey=sunlandszlcx";
            }else{
                str = "apikey=sunlandszlcx&"+p;
            }

            return MD5Util.MD5(str);
            //MessageDigest md = MessageDigest.getInstance("SHA1");
//				return byte2hex(MD5Util.MD5(byte2hex(str.getBytes("UTF-8"))).getBytes());
//	        return byte2hex(md.digest(byte2hex(str.getBytes("UTF-8")).getBytes()));
        } catch (Exception e) {
            throw new RuntimeException("计算签名失败", e);
        }
    }

    public static String getSig1( Map<String, String> params)  {
        String str = null;
        try {
            String p = concatParams3(params);
            if(StringUtils.isEmpty(p)){
                str = "apikey=sunlandszlcx";
            }else{
                str = "apikey=sunlandszlcx&"+p;
            }
        log.info("qianming={}",str);
            return MD5Util.MD5(str);
            //MessageDigest md = MessageDigest.getInstance("SHA1");
//				return byte2hex(MD5Util.MD5(byte2hex(str.getBytes("UTF-8"))).getBytes());
//	        return byte2hex(md.digest(byte2hex(str.getBytes("UTF-8")).getBytes()));
        } catch (Exception e) {
            throw new RuntimeException("计算签名失败", e);
        }
    }



//    public static void main(String[] args){
//        Map<String, String> params = new HashMap<String, String>();
//			/*params.put("refldList","[\n" +
//					"  {\n" +
//					"    \"videoId\" : \"200311245\",\n" +
//					"    \"ablumId\" : \"200014605\",\n" +
//					"    \"favoriteId\" : \"\"\n" +
//					"  }\n" +
//					"]");
//					//https://saasapi.lecloud.com/mobileRegister?mobile=15110227777&password=+U30aji0c3RiRXoJuJ4ilg==&checkCode=123456&nickname=&tenantId=400717&username=15110227777&countryCode=86&email=&version=2&s=1
//					*/
//       /* params.put("mobile","15110227777");
//        params.put("password","+U30aji0c3RiRXoJuJ4ilg==");
//        params.put("checkCode","123456");
//        params.put("mobile","15110227777");
//        params.put("mobile","15110227777");
//        params.put("mobile","15110227777");
//
//*/
//
//        params.put("reportKey","cishu");
//        params.put("reportValue","1");
//        params.put("userOpenid","");
////        params.put("apikey","sunlandszlcx");
//        System.out.println(getSig(params));
//        /*try {
//            System.out.println(MD5Util.MD5("value"));
//        }catch (Exception e){
//            e.printStackTrace();
//        }*/
//
//
//    }
}